﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.DataMining
{
    /// <summary>
    /// This class provides the random selection of data out of a specified matrix.
    /// </summary>
    [Serializable]
    public class RandomMatrixDataSelector : IDataSelector
    {
        /// <summary>
        /// The source matrix for the data selection.
        /// </summary>
        private Matrix sourceMatrix;

        /// <summary>
        /// This field holds the indices for the data selection.
        /// </summary>
        private readonly int[] randomRows;

        /// <summary>
        /// Initializes a new instance of the <see cref="RandomMatrixDataSelector"/> class.
        /// </summary>
        /// <param name="sourceMatrix">The source matrix for the data selection.</param>
        /// <param name="numberOfRows">The number of rows to select.</param>
        public RandomMatrixDataSelector(Matrix sourceMatrix, int numberOfRows)
        {
            if (sourceMatrix == (Matrix)null)
            {
                throw new ArgumentNullException("sourceMatrix");
            }

            if ((sourceMatrix.Rows < numberOfRows) || (numberOfRows <= 0))
            {
                throw new ArgumentException("numberOfRows");
            }

            this.sourceMatrix = sourceMatrix;
            this.randomRows = new int[numberOfRows];

            Random random = new Random();

            for (int i = 0; i < numberOfRows; i++)
            {
                this.randomRows[i] = random.Next(this.sourceMatrix.Rows);
            }
        }

        /// <summary>
        /// Gets or sets the source matrix for the data selection.
        /// </summary>
        /// <value>The source matrix for the data selection.</value>
        public Matrix SourceMatrix
        {
            get { return sourceMatrix; }
            set { sourceMatrix = value; }
        }

        /// <summary>
        /// Gets the value at a specified position.
        /// </summary>
        /// <param name="row">The specified row index.</param>
        /// <param name="column">The specified column index.</param>
        /// <returns>The value at the specified position.</returns>
        public double GetValueAt(int row, int column)
        {
            return this.sourceMatrix.GetValueAtPosition(this.randomRows[row], column);
        }

        /// <summary>
        /// Gets the number of rows of the data set.
        /// </summary>
        /// <returns>The number of rows of the data set.</returns>
        public int GetRowCount()
        {
            return this.randomRows.Length;
        }

        /// <summary>
        /// Gets the number of columns of the data set.
        /// </summary>
        /// <returns>The number of columns of the data set.</returns>
        public int GetColumnCount()
        {
            return this.sourceMatrix.Columns;
        }
    }
}
